package com.meiyou.service.impl;

import com.meiyou.constant.MenstrualConstant;
import com.meiyou.constant.MessageConstant;
import com.meiyou.context.BaseContext;
import com.meiyou.dto.RecordMenstruationDto;
import com.meiyou.entity.MenstrualRecord;
import com.meiyou.exception.MenstrualTypeParamsNot;
import com.meiyou.exception.NoMenstrualStartRecord;
import com.meiyou.mapper.MenstrualRecordMapper;
import com.meiyou.service.MenstrualRecordService;
import com.meiyou.vo.MenstruationVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

/**
 * @BelongsProject: meiyou
 * @BelongsPackage: com.meiyou.service.impl
 * @Author: 尹晓安
 * @Date: 2023/12/4 21:47
 * @Description:
 */
@Service
public class MenstrualRecordServiceImp implements MenstrualRecordService {

    @Autowired
    private MenstrualRecordMapper menstrualRecordMapper;

    /**
     * 记录月经
     *
     * @param recordMenstruationDto
     * @return
     */
    @Override
    public void recordMenstruation(RecordMenstruationDto recordMenstruationDto) {

        // 查看是否有月经类型参数
        if (
                recordMenstruationDto.getType() == null ||
                        (recordMenstruationDto.getType() != MenstrualConstant.END &&
                                recordMenstruationDto.getType() != MenstrualConstant.START)) {

            throw new MenstrualTypeParamsNot(MessageConstant.NO_MENSTRUAL_TYPE_PARAMS);

        }

        MenstrualRecord menstrualRecord = new MenstrualRecord();

        // 月经开始,向表添加数据
        if (recordMenstruationDto.getType() == MenstrualConstant.START) {

            BeanUtils.copyProperties(recordMenstruationDto, menstrualRecord);
            menstrualRecord.setUserId(BaseContext.getCurrentId());
            menstrualRecordMapper.insert(menstrualRecord);

        } else {
            // 月经结束，向表修改数据
            // 查出最新月经数据id
            menstrualRecord = menstrualRecordMapper.selectByTime(BaseContext.getCurrentId());

            // 判断是否有记录
            if (menstrualRecord == null) {

                throw new NoMenstrualStartRecord(MessageConstant.NO_MENSTRUAL_START_RECORD);

            }

            // 开始时间和结束时间一致做删除
            if (menstrualRecord.getStartTime().equals(recordMenstruationDto.getEndTime())) {

                menstrualRecordMapper.remove(menstrualRecord.getId());

            } else {
                menstrualRecord.setStartTime(null);
                menstrualRecord.setEndTime(recordMenstruationDto.getEndTime());
                // 修改表结束时间
                menstrualRecordMapper.update(menstrualRecord);
            }
        }
    }

    /**
     * 获取月经信息
     *
     * @return
     */
    @Override
    public List<MenstruationVo> getMenstruation() {

        List<MenstruationVo> menstruationVoList = menstrualRecordMapper.select(BaseContext.getCurrentId());

        return menstruationVoList;
    }

    /**
     * 查询最新月经结束日期
     *
     * @return
     */
    @Override
    public String queryMenstrualEndLast() {

        String endTime = menstrualRecordMapper.selectEndLast(BaseContext.getCurrentId());

        return endTime;
    }

    /**
     * 查询最新的月经数据
     *
     * @return
     */
    @Override
    public MenstruationVo queryMenstrualLast() {

        MenstruationVo menstruationVo = menstrualRecordMapper.queryLast(BaseContext.getCurrentId());

        return menstruationVo;
    }
}
